package com.hcms.admin.controller.sys;

import com.hcms.admin.annotation.CheckPermission;
import com.hcms.admin.dto.SysMenuOptionResponse;
import com.hcms.admin.dto.SysMenuRouteResponse;
import com.hcms.admin.dto.SysMenuTreeResponse;
import com.hcms.admin.service.ISysMenuService;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ISysMenuService sysMenuService;

    /**
     * 获取路由，用于前端显示
     *
     * @return
     */
    @GetMapping("/routes")
    @ResponseBody
    @CheckPermission(check = false)
    public ResponseEntity<CommonResponse<List<SysMenuRouteResponse>>> routes() {
        List<SysMenuRouteResponse> routes = sysMenuService.getRoutes();
        return CommonResponse.ok(routes);
    }

    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<List<SysMenuTreeResponse>>> index() {
        List<SysMenuTreeResponse> sysMenus = sysMenuService.getTreeList();
        return CommonResponse.ok(sysMenus);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysMenu>> view(@PathVariable Long id) {
        SysMenu menu = sysMenuService.getView(id);
        if (menu == null) {
            return CommonResponse.notFound("记录不存在");
        } else {
            return CommonResponse.ok(menu);
        }
    }

    @PostMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysMenu>> create(@RequestBody SysMenu entity) {
        logger.info("Run create(), name={}", entity.getName());
        int count = sysMenuService.createItem(entity);
        if (count > 0) {
            logger.info("Succeeded in adding record");
            return CommonResponse.created(entity);
        } else {
            logger.error("Failed to add record");
            return CommonResponse.badRequest("添加失败");
        }
    }

    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> update(@PathVariable Long id, @RequestBody SysMenu entity) {
        int count = sysMenuService.updateItem(id, entity);
        if (count > 0) {
            return CommonResponse.accepted("更新成功");
        } else {
            return CommonResponse.badRequest("更新失败");
        }
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@PathVariable String ids) {
        int count = sysMenuService.deleteItem(ids);
        if (count > 0) {
            return CommonResponse.noContent("删除成功");
        } else {
            return CommonResponse.badRequest("删除失败");
        }
    }

    /**
     * 目录Tree数据
     *
     * @return
     */
    @GetMapping("/options")
    @ResponseBody
    @CheckPermission(check = false)
    public ResponseEntity<CommonResponse<List<SysMenuOptionResponse>>> options() {
        List<SysMenuOptionResponse> options = sysMenuService.getOptions();
        return CommonResponse.ok(options);
    }

}
